/* Surface.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Surface class, which is a C++ wrapper for SDL_Surface.

Some of this code assumes that the surface is 32-bit (RGBA channels each 8-bit).
Using the line-drawing functions on surfaces with other bit depths will yield
unexpected results.
*/

#ifndef __MZ_SURFACE_H__
#define __MZ_SURFACE_H__

#include "SDL.h"

#include "Rectangle.h"



class Surface {
public:
	// Default constructor.
	Surface();
	// Create a surface wrapping a raw SDL_Surface.
	// If the SDL_Surface is owned by another Surface in addition to this one,
	// you must manually increment its reference count after using this constructor,
	// or else construct this Surface straight from that other Surface.
	Surface(SDL_Surface *surface);
	// Copy constructor. Makes a shallow copy (sharing the same SDL_Surface).
	Surface(const Surface &surface);
	// Destructor.
	~Surface();

	// Assignment.
	Surface &operator=(const Surface &surface);
	// Check whether an SDL_Surface is associated with this Surface.
	operator bool() const;
	bool operator!() const;

	// Get a "window" into this surface.
	// All drawing into that "window" will be cropped to its bounds.
	Surface Region(int x, int y, int width, int height) const;
	Surface Region(Rectangle rect) const;

	// Make a deep copy of this surface, which can be edited separately from this one.
	Surface MakeCopy() const;
	
	// Get the underlying SDL_Surface.
	SDL_Surface *Data();
	const SDL_Surface *Data() const;

	// Get the dimensions of this Surface.
	int Width() const;
	int Height() const;

	// Map the given RGB value to a pixel value in this surface.
	Uint32 Color(int red, int green, int blue) const;
	Uint32 Color(int greyLevel) const;

	// Draw a horizontal line, using the given color.
	void HLine(int x, int y, int width, Uint32 color);
	// Draw a vertical line, using the given color.
	void VLine(int x, int y, int height, Uint32 color);
	// Draw a frame inside the given rectangle coordinates, using the given color.
	void Frame(int x, int y, int width, int height, Uint32 color);
	void Frame(Rectangle rect, Uint32 color);
	// Fill the rectangle with the specified coordinates.
	void FillRect(int x, int y, int width, int height, Uint32 color);
	void FillRect(Rectangle rect, Uint32 color);
	// Fill in a rectangle with "rounded" corners.
	// (Actually, just 45 degree angle).
	void FillRoundRect(int x, int y, int width, int height, int radius, Uint32 color);
	void FillRoundRect(Rectangle rect, int radius, Uint32 color);
	// Overlay the given color on the given rectangular region.
	void OverlayRect(int x, int y, int width, int height, Uint32 color);
	void OverlayRect(Rectangle rect, Uint32 color);
	// Fill this entire surface with the given color.
	void Erase(Uint32 color);

	// Draw this surface, or a portion of it.
	void Blit(Surface &destination, int x, int y) const;
	void Blit(int sourceX, int sourceY, int width, int height, Surface &destination, int x, int y) const;
	void Blit(Rectangle sourceRect, Surface &destination, int x, int y) const;
	void Blit(Rectangle sourceRect, Surface &destination, Rectangle destinationRect) const;


private:
	// Increment this surface's reference count.
	void Retain();
	// Decrement this surface's reference count and free its memory if this is the last reference to it.
	void Release();


protected:
	// Pointer to the SDL_Surface wrapped by this Surface.
	SDL_Surface *data;
	// Region of that bitmap that is considered part of this surface.
	Rectangle bounds;


template <typename Type> friend class ConstPixMapT;
};



#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
